import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../views/Login/index.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login", //登录页
    name: "login",
    component: Login,
  },
  {
    path: "/home", // 左侧菜单栏
    name: "home",
    redirect: "/welcome",
    meta: {
      auth: true,
    },
    component: () => import("../views/Home"),
    children: [
      {
        path: "/welcome", //进入欢迎页
        name: "welcome",
        component: () => import("../views/Home/Welcome"),
      },
      {
        path: "/users", //用户列表
        name: "users",
        component: () => import("../views/Home/Users"),
      },
      {
        path: "/roles", //角色列表
        name: "roles",
        component: () => import("../views/Home/Rights/roles.vue"),
      },

      {
        path: "/rights", //权限列表
        name: "rights",
        component: () => import("../views/Home/Rights/rights.vue"),
      },
      {
        path: "/goods", //商品列表
        name: "goods",
        component: () => import("../views/Home/Goods/GoodsList.vue"),
      },
      {
        path: "/add", //商品列表
        name: "add",
        component: () => import("../views/Home/Goods/GoodsAdd.vue"),
      },
      {
        path: "/params", //分类参数
        name: "params",
        component: () => import("../views/Home/Goods/Goodsparams.vue"),
      },
      {
        path: "/categories", //商品分类
        name: "categories",
        component: () => import("../views/Home/Goods/Goodscategories.vue"),
      },
      {
        path: "/reports", //数据统计
        name: "reports",
        component: () => import("../views/Home/Reports"),
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});
// router.beforeEach((to, form, next) => {
//   console.log(to, "111");
//   console.log(form, "2222");
//   const flag = to.matched.some((item) => item.meta.auth);
//   if (flag) {
//     //需要鉴权
//     const token = sessionStorage.getItem("token");
//     if (!token) {
//       //如果没有token跳转login
//       return next( to.path=="/login" );
//     }
//     //如果有token，放行
//     next();
//   }
//   next();
// });
router.beforeEach((to, from, next) => {
  if (!sessionStorage.getItem("token") && to.path !== "/login") {
    next("/login");
  } else {
    next();
  }
});
export default router;
